package com.it.auth;

import com.it.vo.MenuVo;
import com.it.vo.RoleVo;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class LoginConvert {

    public static List<Roles> toRoles(List<RoleVo> roles) {
        if (roles == null) {
            return null;
        }
        return roles.stream()
                .filter(Objects::nonNull) // 过滤掉null的Role对象
                .map(role -> new Roles(
                        role.getName(),       // 设置id
                        Collections.singletonList(role.getCode()) // 设置operation
                ))
                .collect(Collectors.toList());
    }

    public static List<Permissions> toPermissions(List<MenuVo> menus) {
        if (menus == null) {
            return null;
        }
        return menus.stream()
                .filter(Objects::nonNull) // 过滤掉 null 的 Menu
                .map(menu -> {
                    Permissions vo = new Permissions();
                    vo.setId(menu.getAuthority());
                    vo.setOperation(
                            menu.getChildren() == null || menu.getChildren().isEmpty()
                                    ? Collections.emptyList()
                                    : menu.getChildren().stream()
                                    .map(MenuVo::getAuthority)
                                    .collect(Collectors.toList())
                    );
                    return vo;
                })
                .collect(Collectors.toList());
    }
}
